package com.reading.cms.controller.system;

import com.reading.cms.controller.BaseController;
import com.reading.common.entity.system.Dept;
import com.reading.core.manager.system.IDeptManager;
import com.reading.common.utils.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 部门信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    private String prefix = "system/dept";

    @Autowired
    private IDeptManager deptManager;

    @RequiresPermissions("system:dept:view")
    @GetMapping()
    public String dept() {
        return prefix + "/dept";
    }

    @RequiresPermissions("system:dept:list")
    @GetMapping("/list")
    @ResponseBody
    public List<Dept> list() {
        List<Dept> deptList = deptManager.selectDeptAll();
        return deptList;
    }

    /**
     * 修改
     */
    @RequiresPermissions("system:dept:edit")
    @GetMapping("/edit/{deptId}")
    public String edit(@PathVariable("deptId") Integer deptId, Model model) {
        Dept dept = deptManager.selectDeptById(deptId);
        model.addAttribute("dept", dept);
        return prefix + "/edit";
    }

    /**
     * 新增
     */
    @RequiresPermissions("system:dept:add")
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Integer parentId, Model model) {
        Dept dept = deptManager.selectDeptById(parentId);
        model.addAttribute("dept", dept);
        return prefix + "/add";
    }

    /**
     * 保存
     */
    @RequiresPermissions("system:dept:save")
    @PostMapping("/save")
    @ResponseBody
    public JsonResult save(Dept dept) {
        if (deptManager.saveDept(dept) > 0) {
            return success();
        }
        return error();
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:dept:remove")
    @PostMapping("/remove/{deptId}")
    @ResponseBody
    public JsonResult remove(@PathVariable("deptId") Integer deptId) {
        if (deptManager.selectDeptCount(deptId) > 0) {
            return error(1, "存在下级部门,不允许删除");
        }
        if (deptManager.checkDeptExistUser(deptId)) {
            return error(1, "部门存在用户,不允许删除");
        }
        if (deptManager.deleteDeptById(deptId) > 0) {
            return success();
        }
        return error();
    }

    /**
     * 校验部门名称
     */
    @PostMapping("/checkDeptNameUnique")
    @ResponseBody
    public String checkDeptNameUnique(Dept dept) {
        String uniqueFlag = "0";
        if (dept != null) {
            uniqueFlag = deptManager.checkDeptNameUnique(dept);
        }
        return uniqueFlag;
    }

    /**
     * 选择部门树
     */
    @GetMapping("/selectDeptTree/{deptId}")
    public String selectDeptTree(@PathVariable("deptId") Integer deptId, Model model) {
        model.addAttribute("treeName", deptManager.selectDeptById(deptId).getDeptName());
        return prefix + "/tree";
    }

    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Map<String, Object>> treeData() {
        List<Map<String, Object>> tree = deptManager.selectDeptTree();
        return tree;
    }
}
